home *** CD-ROM | disk | FTP | other *** search
- Path: EU.net!sun4nl!ittpub!ittpub!nntp
- Newsgroups: comp.lang.c++
- Subject: Re: GCC and Default Constructors
- Message-ID: <1996Jan23.105906.1752@ittpub>
- From: wil@ittpub.nl (Wil Evers)
- Date: 23 Jan 96 10:59:06 WET
- References: <4dtv71$1eh@news1.usa.pipeline.com>
- Distribution: world
- Nntp-Posting-Host: lintilla
-
- In article <4dtv71$1eh@news1.usa.pipeline.com> grantp@usa.pipeline.com
- writes:
- > On Jan 20, 1996 17:29:29 in article <GCC and Default Constructors>,
- > 'Eric Richard <erichard@netgen.com>' wrote:
- >
- > >I just spent about 3 days intermittently trying to solve a problem that
- > >I finally figured out and I was trying to see if there is something
- > >I can do to avoid a similar situation.
- > >
- > >Basically my problem stemmed from a typo on my part and the fact
- > >that C++ will automatically provide a default constructor for you if
- > >you don't provide one. To make a long story short, I had a class
- > >named 'QueryTable' and had defined a constructor
- > >'queryTable::QueryTable'.
- >
- > >Now obviously this is wrong, but what was screwing me up was that no
- > >error was being generated because G++ was happily providing me with
- > >a default constructor for my class.
- >
- > Are you saying that the compiler accepted a function definition
- > for an undeclared class??? Any decent compiler -- and gcc/g++
- > certainly is decent -- should not only warn you, but refuse to compile
- > the file. Maybe you've left out something relevant. Or maybe you
- > have another typo.
-
- However, a similar error sometimes bites me:
-
- class ClassName {
- public :
- Classname(const ClassName&) { ... }
- // ^--- lower case 'n': not a copy constructor,
- // but a regular member function
- };
-
- > >So, my question is this: Does anybody know if there is some way
- > >of at least getting G++ to warn you if it is using a default
- > >constructor? If I had that, I would've been able to figure this out
- > >much more quickly.
- > >
- > No, there isn't, and I'm not convinced that there ought to be one.
- > Like the old saw: Too many warnings spoil the stew -- or maybe I
- > now have a typo :-).
- >
- > Excessive warnings defeat their purpose. IMHO, warnings about
- > default constructors borderlines on the excessive.
-
- Agreed, such a warning would be very annoying, especially when including
- perfectly valid class definitions written by other people. However, as a
- matter of routine, I always consider what should happen if someone tries
- to copy an object of a class I wrote myself, and I make sure my classes
- either have a defined copy constructor and assignment operator (even when
- the compiler would have generated the same code), or I disable them by
- declaring them private.
-
- Sometimes, when in a paranoid mood, I use the following helper class:
-
- class NoGeneratedCopying {
- private :
- NoGeneratedCopying(const NoGeneratedCopying&);
- void operator=(const NoGeneratedCopying&);
- public :
- NoGeneratedCopying() { }
- };
-
- If you put a 'NoGeneratedCopying' data member in a class, the compiler is
- not allowed to generate a copy constructor or assignment operator for that
- class, and unless you explicitly provide them, an attempt to copy an
- object of that class will result in a compile-time error.
-
- - Wil
-